$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'client/manager/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', key: true,width:70 },
			{ label: '用户名', name: 'userName',index:"user_name",width:100,align:'center' },
			{ label: '手机号', name: 'mobileNo',index:"mobile_no" ,width:100,align:'center' },
			{ label: 'vip', name: 'vipType',index:"vip_type" ,width:100,align:'center', 
				formatter:function(value,options,rowObject){
					if(rowObject.vipType==1){
						return "月卡";
					}else if(rowObject.vipType==2){
						return "年卡";
					}else{
						return "";
					}
				}
			},
			{ label: '押金金额', name: 'depositAmount', index:"deposit_amount",width:80 ,align:'center'},
			{ label: '账户余额', name: 'cashAmount',index:"cash_amount",width:80 ,align:'center' },
			{ label: '书柜号', name: 'shelfCode',index:"shelf_code",width:100 ,align:'center' },
			{ label: '借阅图书数量', name: 'bookCount',index:"book_count" ,width:100 ,align:'center',
				formatter:function(value,options,rowObject){
					if(rowObject.bookCount==null){
						rowObject.bookCount = 0;
					}
					return '<a onclick=\"getAlreadyBorrowBooksHtml('+rowObject.id+')\">'+rowObject.bookCount+'</a>';
				}
			},
			{ label: '在借图书数量', name: 'borrowCount',index:"borrow_count",width:100 ,align:'center',
				formatter:function(value,options,rowObject){
					if(rowObject.borrowCount==null){
						rowObject.borrowCount = 0;
					}
					return '<a onclick=\"getCurrentBorrowBooksHtml('+rowObject.id+')\">'+rowObject.borrowCount+'</a>';
				}
			},
			{ label: '积分', name: 'pointValue',index:"point_value",width:60,align:'center',
				formatter:function(value,options,rowObject){
					return '<a onclick=\"getPointValueHtml('+rowObject.id+')\">'+rowObject.pointValue+'</a>';
				}
			},
			{ label: '信用分', name: 'creditValue',index:"credit_value",width:60,align:'center',
				formatter:function(value,options,rowObject){
					return '<a onclick=\"getCreditValueHtml('+rowObject.id+')\">'+rowObject.creditValue+'</a>';
				}
			},
			{ label: '状态', name: 'cashStatus', index:"status", width: 80 ,align:'center',
				formatter:function(value,options,rowObject){
					if(value==0){
						return "新注册";
					}if(value==2){
						return "正常";
					}else if(value==6){
						return "已退押金";
					}else if(value==7){
						return "退款失败";
					}else if(value==10){
						return "欠费";
					}else if(value==11){
						return "严重欠费";
					}else {
						return "资金状态不正常";
					}
				}
			,sortable:false},
			{ label: '用户类型', name: 'userType',index:"user_type",width:70,align:'center',formatter:function(value,options,rowObject){
				if(value==0){return "<a onclick=\"updateUserType("+rowObject.id+")\">正常</a>"}else if(value==1){return "<a onclick=\"updateUserType("+rowObject.id+")\">运维人员</a>"}
			}},
			/*{ label: '黑名单', name: 'id',index:"id",width:60 ,align:'center'},*/
			{ label: '注册时间', name: 'createTime',index:"create_time" ,align:'center'},
			{ label: '最后登录时间', name: 'tokenGenerateTime',index:"token_generate_time",align:'center'},
			{ label: '评论数量', name: 'commentCount',index:"comment_count",width:100,align:'center' },
			{ label: '报修次数', name: 'repairsNum',index:"repairs_num",width:100 ,align:'center',
				formatter:function(cellvalue, options, rowObject){
					return '<a onclick=\"getMaintainCount('+rowObject.id+')\">'+rowObject.repairsNum+'</a>'
				}
			,sortable:false},
			{ label: '操作', name: 'id', width: 150 ,formatter:function(cellvalue, options, rowObject){  
				return "<a onclick=\"updateCreditValue("+rowObject.id+",\'"+rowObject.userName+"\',"+rowObject.mobileNo+","+rowObject.creditValue+")\">设置信用分</a> <a  onclick=\"updatePointValue("+rowObject.id+",\'"+rowObject.userName+"\',"+rowObject.mobileNo+","+rowObject.pointValue+")\">设置积分</a>";
			},sortable:false,align:'center'}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        autoScroll:true,
        shrinkToFit:false,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        }
        
    });
    /*创建jqGrid的操作按钮容器*/
    /*可以控制界面上增删改查的按钮是否显示*/
    //jQuery("#jqGrid").jqGrid('navGrid', '#jqGridPager', {edit : true,add : true,del : true});
    //表格宽度自适应
    $(function(){
       $(window).resize(function(){
          $("#jqGrid").setGridWidth($(window).width());
       });
    });
});


var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userName: null,
			mobileNo: null,
			statusCash: null,
			registerStartTime:null,
			registerEndTime:null
		},
		showList: true,
		title: null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		generator: function() {
			var tableNames = getSelectedRows();
			if(tableNames == null){
				return ;
			}
			location.href = baseURL + "sys/generator/code?tables=" + JSON.stringify(tableNames);
		},
		getInfo: function(id){
			$.get(baseURL + "accountcash/info/"+id, function(r){
                vm.accountCash = r.accountCash;
            });
		},
		updateUserType:function(id){
			//debugger;
			layer.open({
			    type: 1
			    ,title: "修改用户类型" //不显示标题栏   title : false/标题
			    ,closeBtn: true
			    ,area: '300px;'
			    ,shade: 0.8
			    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
			    ,resize: false
			    ,btn: ['正常', '运维人员','取消']
			    ,btnAlign: 'c'
			    ,moveType: 1 //拖拽模式，0或者1
			    ,content: ''
			    ,btn1:function(index,layero){
			    	var jsonData = {"id":id,"userType":0};
			    	ajaxPost('client/manager/update',jsonData,'用户类型修改成功');
			       	layer.close(layer.index);
			    },btn2:function(index,layero){
			    	var jsonData = {"id":id,"userType":1};
			    	ajaxPost('client/manager/update',jsonData,'用户类型修改成功');
			    	layer.close(layer.index);
			    },btn3: function(){ 
			        //右上角关闭回调
			        //return false 开启该代码可禁止点击该按钮关闭
			    },cancel: function(){ 
				    //右上角关闭回调
				        
				    //return false 开启该代码可禁止点击该按钮关闭
				}
			});
		},
		updateCreditValue:function(id,userName,mobileNo,creditValue){
			layer.open({
			    type: 1
			    ,title: "设置信用分" //不显示标题栏   title : false/标题
			    ,closeBtn: true
			    ,area: ['300px','300px']
			    ,shade: 0.8
			    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
			    ,resize: false
			    ,btn: ['提交', '取消']
			    ,btnAlign: 'c'
			    ,moveType: 1 //拖拽模式，0或者1
			    ,content: '<div>手机号:'+mobileNo+'&nbsp;&nbsp;用户名:'+userName+'<br/><br/>当前信用分:'+creditValue+'<br/><br/>'+'新增信用分:<input id="creditValue" type="text" /><br/>备注:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea id="detail" rows="4" cols="21"> </textarea></div>'//'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+mobileNo+'<br/><input id="creditValue" type="text" /> </div>'
			    ,yes:function(index,layero){
			    	var currentCreditPoint = document.getElementById('creditValue').value;
			    	var r = /(^\d+$)/;
			    	if(!r.test(currentCreditPoint)){
			    		layer.msg("请输入正确的信用分。");
			    		return;
			    	}
			    	var detail  = document.getElementById('detail').value;
			    	var jsonData = {"id":id,"creditValue":currentCreditPoint,"detail":detail};
			    	ajaxPost('client/manager/update',jsonData,'信用分修改成功')
			       	layer.close(layer.index);
			    },cancel: function(){ 
			        //右上角关闭回调
			        
			        //return false 开启该代码可禁止点击该按钮关闭
			      }
			});
		},
		updatePointValue:function(id,userName,mobileNo,pointValue){
			layer.open({
			    type: 1
			    ,title: "设置积分" //不显示标题栏   title : false/标题
			    ,closeBtn: true
			    ,area: ['300px','300px']
			    ,shade: 0.8
			    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
			    ,resize: false
			    ,btn: ['提交', '取消']
			    ,btnAlign: 'c'
			    ,moveType: 1 //拖拽模式，0或者1
			    ,content: '<div>手机号:'+mobileNo+'&nbsp;&nbsp;用户名:'+userName+'<br/><br/>当前积分:'+pointValue+'<br/><br/>'+'新增积分:<input id="pointValue" type="text" /><br/>备注:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea id="detail" rows="4" cols="21"> </textarea></div>'//'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+mobileNo+'<br/><input id="creditValue" type="text" /> </div>'
			    ,yes:function(index,layero){
			    	var newPointValue = document.getElementById("pointValue").value;
			    	var r = /(^\d+$)/;
			    	if(!r.test(newPointValue)){
			    		console.log(newPointValue);
			    		layer.msg("请输入正确的积分。");
			    		return;
			    	}
			    	var detail = document.getElementById('detail').value;
					var jsonData = {"id":id,"pointValue":newPointValue,"detail":detail};
					ajaxPost('client/manager/update',jsonData,"积分修改成功");
					layer.close(layer.index);
			    },cancel: function(){ 
			        //右上角关闭回调
			        
			        //return false 开启该代码可禁止点击该按钮关闭
			      }
			});
			
			/*layer.confirm('设置积分,手机号：'+mobileNo+',积分:'+pointValue+'<input id="pointValue" type="text" value=\"'+id+'\" />', {
				btn : [ '提交', '取消' ]
					//按钮
			}, function() {
				var newPointValue = document.getElementById("pointValue").value;
				var jsonData = {"id":id,"pointValue":newPointValue};
				ajaxPost('client/manager/update',jsonData,"积分修改成功");
				layer.close(layer.index);
			}, function() {
				
			});*/
		},
		addUser: function(){
			layer.confirm('添加用户:<br/>请输入用户手机号：<input id="mobileNo" type="text" />', {
				btn : [ '提交', '取消' ]
					//按钮
			}, function() {
				var mobileNo = document.getElementById("mobileNo").value;
				
				var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
		        if (!myreg.test(mobileNo)) {  
		            layer.msg("请输入正确手机号。");
		            return;
		        }
				var jsonData = {"mobileNo":mobileNo};
				ajaxPost('client/manager/insert',jsonData,'添加成功');
			}, function() {
				layer.close(layer.index);
			});
		},
		getMaintainCount:function(id){
			layer.open({
				type : 2,
				title : '报修详细',
				closeBtn: 1,
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '900px', '594px' ],
				content : ['./client-maintain.html?id=' + id,'no']
			});
		},
		getCurrentBorrowBooks: function(id){
			layer.open({
				type : 2,
				title : '在借图书查询',
				closeBtn: 1,
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '1200px', '700px' ],
				content : ['./client-book.html?user_id=' + id + '&trans_status=20','no']
			});
		},
		getAlreadyBorrowBooks: function(id){
			layer.open({
				type : 2,
				title : '已借图书查询',
				closeBtn: 1,
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '1200px', '700px' ],
				content : ['./client-book.html?user_id=' + id + '&trans_status=30','no']
			});
		},
		getPointValue: function(id){
			layer.open({
				type : 2,
				title : '用户积分流水',
				closeBtn: 1,
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '700px', '650px' ],
				content : ['./client-point-record.html?user_id=' + id ,'no']
			});
		},
		getCreditValue: function(id){
			layer.open({
				type : 2,
				title : '用户积分流水',
				closeBtn: 1,
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '700px', '650px' ],
				content : ['./client-credit-record.html?user_id=' + id ,'no']
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'mobileNo' : vm.q.mobileNo,
					'userName' : vm.q.userName,
					'cashStatus' : vm.q.statusCash,
					'registerStartTime' : vm.q.registerStartTime,
					'registerEndTime' : vm.q.registerEndTime,
					'shelfCode' : vm.q.shelfCode
				},
				page : page
			}).trigger("reloadGrid");
		}
	}
});